package com.lsfb.signln.Location.TaskMsg.Leave;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lsfb.signln.Login.LoginActivity;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.LittleUtils;
import com.lsfb.signln.Utils.MyProgressDialog;
import com.lsfb.signln.Utils.PopWindowManager;
import com.lsfb.signln.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.lsfb.signln.R.id.aty_leave_bottom;

/**
 * 请假详情
 * Created by Administrator on 2018/1/25 0025.
 */
@ContentView(R.layout.aty_leave)
public class LeaveActivity extends LsfbActivity {

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;

    /**
     * 头像
     */
    @ViewInject(R.id.aty_leave_img_head)
    private SimpleDraweeView img_head;
    @ViewInject(R.id.aty_leave_head)
    private RelativeLayout rl_leave_head;
    @ViewInject(R.id.aty_leave_text_head)
    private TextView tv_head;

    /**
     * 名字
     */
    @ViewInject(R.id.aty_leave_tv_name)
    private TextView tv_name;

    /**
     * 部门
     */
    @ViewInject(R.id.aty_leave_tv_department)
    private TextView tv_department;

    /**
     * 审批状态
     */
    @ViewInject(R.id.aty_leave_iv_status)
    private ImageView iv_status;
    @ViewInject(R.id.aty_leave_tv_status)
    private TextView tv_status;

    /**
     * 请假类型
     */
    @ViewInject(R.id.aty_leave_tv_type)
    private TextView tv_type;

    /**
     * 请假天数
     */
    @ViewInject(R.id.aty_leave_tv_days)
    private TextView tv_days;

    /**
     * 请假时间
     */
    @ViewInject(R.id.aty_leave_tv_time)
    private TextView tv_time;

    /**
     * 请假事由
     */
    @ViewInject(R.id.aty_leave_tv_reason)
    private TextView tv_reason;
    @ViewInject(R.id.aty_leave_img_reason)
    private SimpleDraweeView img_reason;

    /**
     * 审批按钮 同意/拒绝
     */
    @ViewInject(aty_leave_bottom)
    private LinearLayout ll_bottom;

    /**
     * 撤销申请
     */
    @ViewInject(R.id.aty_leave_text_cancel)
    private TextView text_cancel;

    /**
     * 审批进度
     */
    @ViewInject(R.id.rv_progress)
    private RecyclerView rv_progress;
    private List<LeaveDetailsBean.ListBean> progress_datas;
    private LeaveDetailsAdapter adapter;

    private MyProgressDialog dialog;

    private String lid;

    @ViewInject(R.id.bottom)
    private RelativeLayout bottom;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "免定位详情");

        if (getIntent().getStringExtra("lid") != null) {
            lid = getIntent().getStringExtra("lid");
        } else {
            T.showShort(this, "获取数据异常");
            this.finish();
            return;
        }

        dialog = new MyProgressDialog(this, "加载中...");

        progress_datas = new ArrayList<>();
        adapter = new LeaveDetailsAdapter(this, progress_datas, R.layout.item_leave_progress);
        rv_progress.setLayoutManager(new LinearLayoutManager(this));
        rv_progress.setAdapter(adapter);

        getDetails();
    }

    /**
     * 获取请假详情
     */
    private void getDetails() {
        HashMap hashMap = new HashMap();
        hashMap.put("lid", lid);
        dialog.showdialog();
        new BaseInternet().getData_Get(URLString.LEAVEDETAILS, hashMap, LeaveDetailsBean.class, new LeaveDetailsEvent<LeaveDetailsBean>(), true, false);
    }

    @EventAnnotation
    public void eventResult(LeaveDetailsEvent<LeaveDetailsBean> event) {
        dialog.hidedialog();
        switch (event.getCode()) {
            case 200:
                switch (event.getData().getPermission()) {
                    case 2:     //申请人
                        bottom.setVisibility(View.VISIBLE);
                        ll_bottom.setVisibility(View.GONE);
                        text_cancel.setVisibility(View.VISIBLE);
                        break;
                    case 3:     //审批人
                        bottom.setVisibility(View.VISIBLE);
                        ll_bottom.setVisibility(View.VISIBLE);
                        text_cancel.setVisibility(View.GONE);
                        break;
                    case 1:     //抄送人
                    case 4://审批完，可查看，无操作
                        bottom.setVisibility(View.GONE);
                        ll_bottom.setVisibility(View.GONE);
                        text_cancel.setVisibility(View.GONE);
                        break;
                }

                setData(event.getData());
                if (event.getData().getList() != null && !event.getData().getList().isEmpty()) {
                    progress_datas.clear();
                    progress_datas.addAll(event.getData().getList());
                    adapter.notifyDataSetChanged();
                }
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    /**
     * 给控件赋值
     *
     * @param bean
     */
    private void setData(LeaveDetailsBean bean) {
        //头像
        if (bean.getHeadImg() != null && !bean.getHeadImg().isEmpty()) {
            img_head.setVisibility(View.VISIBLE);
            rl_leave_head.setVisibility(View.GONE);
            img_head.setImageURI(URLString.IMAGES + bean.getHeadImg());
        } else {
            img_head.setVisibility(View.GONE);
            rl_leave_head.setVisibility(View.VISIBLE);
            tv_head.setText(bean.getName().substring(bean.getName().length() - 2, bean.getName().length()));

        }
        //姓名
        tv_name.setText(bean.getName());
        //部门
        tv_department.setText(bean.getDeparName());
        //审批状态
        switch (bean.getState()) {
            case -3:    //取消
                iv_status.setImageResource(R.mipmap.img_refuse);
                tv_status.setText("已撤销");
                text_cancel.setVisibility(View.GONE);
                break;
            case -2:    //拒绝
                iv_status.setImageResource(R.mipmap.img_refuse);
                tv_status.setText("被拒绝");
                tv_status.setTextColor(getResources().getColor(R.color.label_red));
                break;
            case -1:    //成功
                iv_status.setImageResource(R.mipmap.img_pass);
                tv_status.setText("已通过");
                tv_status.setTextColor(getResources().getColor(R.color.label_forgive));
                break;
            default:    //正在审核
                iv_status.setImageResource(R.mipmap.img_wait);
                tv_status.setText("待审批");
                break;
        }

        //请假类型
        switch (bean.getType()) {
            case 0:     //事假
                tv_type.setText("因事");
                break;
            case 1:     //病假
                tv_type.setText("因病");
                break;
            case 2:     //年休假
                tv_type.setText("年休");
                break;
            case 3:     //其他
                tv_type.setText("其他");
                break;
        }
        //请假天数
        tv_days.setText("免定位天数：" + String.valueOf(bean.getDays()) + "天");
        //请假时间
        tv_time.setText("免定位时间：" + bean.getTimes());
        //请假事由
        tv_reason.setText("免定位原因：" + bean.getContent());
        if (bean.getImgPath() != null) {
            img_reason.setVisibility(View.VISIBLE);
            img_reason.setImageURI(URLString.IMAGES + bean.getImgPath());
        } else {
            img_reason.setVisibility(View.GONE);
        }
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_leave_text_refuse, R.id.aty_leave_text_pass, R.id.aty_leave_text_cancel})
    public void onClick(View view) {
        switch (view.getId()) {
            //拒绝
            case R.id.aty_leave_text_refuse:
                replay = new PopWindowManager(this, R.layout.pop_refuse) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final EditText et_import = (EditText) contentView.findViewById(R.id.pop_et_import);
                        final TextView pop_no = (TextView) contentView.findViewById(R.id.pop_no);
                        final TextView pop_yes = (TextView) contentView.findViewById(R.id.pop_yes);

                        pop_no.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //输入完成后收起软键盘
                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.hideSoftInputFromWindow(et_import.getWindowToken(), 0);

                                manager.hideWindow();
                            }
                        });

                        pop_yes.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //输入完成后收起软键盘
                                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.hideSoftInputFromWindow(et_import.getWindowToken(), 0);
                                if (et_import.getText().toString().equals("请填写拒绝理由") || et_import.getText().toString().length() == 0) {
                                    T.showShort(getApplicationContext(), "请填写拒绝理由");
                                } else {
                                    approval(0, et_import.getText().toString());
                                    manager.hideWindow();
                                }

                            }
                        });

                    }
                };
                replay.showWindows(getWindow().getDecorView());
                break;
            //同意
            case R.id.aty_leave_text_pass:
                approval(1, "");
                break;
            //撤销
            case R.id.aty_leave_text_cancel:
                approval(2, "");
                break;
        }
    }

    /**
     * 审批
     */
    private void approval(int code, String content) {
        HashMap hashMap = new HashMap();
        hashMap.put("tid", lid);
        hashMap.put("results", String.valueOf(code));
        if (code == 0) {
            hashMap.put("content", content);
        }
        new BaseInternet().getData(URLString.APPROVAL, hashMap, String.class, new ApprovalEvent<String>(), true);
    }

    @EventAnnotation
    public void eventResult(ApprovalEvent<String> event) {
        switch (event.getCode()) {
            case 200:
                this.finish();
//                T.showShort(this, "已同意");
                break;
            case 1006:
                T.showShort(this, "您的账号已在其它设备登录，请重登录");
                LittleUtils.exit(this);
                startActivity(new Intent(this, LoginActivity.class));
                break;
            default:
                T.showShort(this, event.getMsg());
                break;
        }
    }

    @Override
    public void initData() {

    }
}
